<template>
  <div class="container">
    <div class="app-container">
      <el-button
        size="mini"
        type="primary"
        style="margin: 10px"
        @click="addType1()"
        >添加权限</el-button
      >
      <el-table
        :data="tableData"
        style="width: 100%; margin-bottom: 20px"
        row-key="id"
        default-expand-all
      >
        <el-table-column prop="name" label="名称"> </el-table-column>
        <el-table-column prop="code" label="标识"> </el-table-column>
        <el-table-column prop="description" label="描述"> </el-table-column>
        <el-table-column label="操作">
          <template v-slot="{ row }">
            <el-button
              v-if="row.type === 1"
              type="text"
              size="mini"
              @click="addType2(row.id)"
              >添加</el-button
            >
            <el-button type="text" size="mini" @click="editFn(row.id)"
              >编辑</el-button
            >
            <el-button type="text" size="mini" @click="delFn(row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 添加权限弹窗 -->
      <el-dialog
        :title="title"
        :visible.sync="addFormVisible"
        @close="closeFn"
        width="500px"
      >
        <el-form
          ref="addForm"
          :model="addForm"
          :rules="rules"
          label-width="120px"
        >
          <el-form-item label="权限名称" prop="name">
            <el-input
              v-model="addForm.name"
              autocomplete="off"
              style="width: 300px"
            ></el-input>
          </el-form-item>
          <el-form-item label="权限标识" prop="code">
            <el-input
              v-model="addForm.code"
              autocomplete="off"
              style="width: 300px"
            ></el-input>
          </el-form-item>
          <el-form-item label="权限描述" prop="description">
            <el-input
              style="width: 300px"
              type="textarea"
              :rows="4"
              placeholder="请输入内容"
              v-model="addForm.description"
            >
            </el-input>
          </el-form-item>
          <el-form-item label="开启">
            <el-switch
              v-model="addForm.enVisible"
              active-color="#409eff"
              inactive-color="#dcdfe6"
              :active-value="1"
              :inactive-value="0"
            >
            </el-switch>
          </el-form-item>
          <el-row type="flex" justify="center">
            <el-col :span="7">
              <el-button type="primary" size="mini" @click="submit"
                >确 定</el-button
              >
              <el-button size="mini" @click="closeFn">取 消</el-button>
            </el-col>
          </el-row>
        </el-form>
      </el-dialog>
      <!-- 关闭警告弹窗 -->
      <el-dialog
        title="提示"
        :visible.sync="dialogVisible"
        width="30%"
      >
        <span>确定要删除该数据吗？</span>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="confirmDel"
            >确 定</el-button
          >
        </span>
      </el-dialog>
    </div>
  </div>
</template>
<script>
import {
  getPermissionListApi,
  addPermissionApi,
  getPermissionInfoApi,
  editPermissionInfoApi,
  delPermissionInfoApi,
} from "@/api/permission";
import { transListToTree } from "@/utils/index";
export default {
  name: "Permission",
  data() {
    return {
      tableData: [],
      title: "",
      addFormVisible: false,
      dialogVisible: false,
      currentId:'',
      addForm: {
        name: "",
        code: "",
        description: "",
        enVisible: "",
        pid: "",
        type: "",
      },
      rules: {
        name: [
          { required: true, message: "权限名称不能为空", trigger: "blur" },
        ],
        code: [
          { required: true, message: "权限标识不能为空", trigger: "blur" },
        ],
        description: [
          { required: true, message: "权限描述不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getPermissionList();
  },
  methods: {
    async getPermissionList() {
      const res = await getPermissionListApi();
      this.tableData = transListToTree(res, 0);
    },
    addType1() {
      // 顶栏添加
      this.title = "新增权限点";
      this.addForm.type = 1;
      this.addForm.pid = 0;
      this.addFormVisible = true;
    },
    addType2(id) {
      // 按钮添加
      this.title = "新增权限点";
      this.addForm.type = 2;
      this.addForm.pid = id;
      this.addFormVisible = true;
    },
    submit() {
      this.$refs.addForm.validate(async (pass) => {
        if (pass) {
          if (this.title === "新增权限点") {
            await addPermissionApi(this.addForm);
            this.$message.success("添加权限点成功");
          } else {
            await editPermissionInfoApi(this.addForm);
            this.$message.success("修改权限点成功");
          }
          this.closeFn();
          this.getPermissionList();
        }
      });
    },
    async editFn(id) {
      this.title = "编辑权限点";
      this.addForm = await getPermissionInfoApi(id);
      this.addFormVisible = true;
    },
    delFn(id){
      this.currentId = id
      this.dialogVisible = true
    },
    async confirmDel(){
      await delPermissionInfoApi(this.currentId)
      this.$message.success('删除成功')
      this.dialogVisible = false
      this.getPermissionList();
    },
    closeFn() {
      this.addFormVisible = false;
      this.$refs.addForm.resetFields();
      this.addForm = {
        name: "",
        code: "",
        description: "",
        enVisible: "",
        pid: "",
        type: "",
      };
    },
  },
};
</script>
